package com.oocl.communicator.impl;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.text.ParseException;

import org.json.JSONException;
import org.json.JSONObject;

import com.oocl.communicator.Receiver;
import com.oocl.constant.LogLevel;
import com.oocl.pojo.Protocol;
import com.oocl.util.ConsoleLogUtil;
import com.oocl.util.JsonUtil;

public class ReceiverImpl implements Receiver{

	@Override
	public Protocol receive(BufferedReader in) throws IOException, ClassNotFoundException {
		Protocol protocol = null;
		try {
			String jsonStr =in.readLine().trim();
			protocol = JsonUtil.toProtocol(new JSONObject(jsonStr));
		} catch (EOFException e) {
			ConsoleLogUtil.log("Server EOFException! "+e.getMessage(), LogLevel.ERROR);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return protocol;
	}
	
}
